#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QWebSocketServer>
#include<QWebSocket>
#include<QJsonObject>
#include<QJsonDocument>
#include<QJsonArray>
#include<QFile>
#include<QTimer>
#include<QThreadPool>
#include<QMap>
#include"TcpServer.h"
#include<QMutex>
#include<QMessageBox>
QMutex* m_mutex=new QMutex();//锁webSocketM变量，保证只有一个线程可以访问该变量
QMap<QWebSocket*,int> webSocketM;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->TcpServerContent->setText("10001");
    ui->WebSocketContent->setText("10000");
    connect(this,&MainWindow::sendC,this,[=](QString s,SendContent t){
        if(t==SendContent::CONNECT){
            ui->ConnectStu->append("<font color=green>"+s+"</font>");
        }else{
            ui->ConnectStu->append("<font color=red>"+s+"</font>");
        }
    });
}

void MainWindow::newClient(){
    qDebug()<<"山海鲸发送连接请求";
    QWebSocket* webSock=this->webSocketServer->nextPendingConnection();
    emit(this->sendC("山海鲸连接成功...",SendContent::CONNECT));//连接成功
    ui->ConnectStu->addAction("山海经客户端发送连接请求...");
    m_mutex->lock();
    webSocketM[webSock]=1;//将用于通信的套接字存储起来
    m_mutex->unlock();
    connect(webSock,&QWebSocket::disconnected,this,[=](){
        disWebSocket(webSock);
    });
}

void  MainWindow::disWebSocket(QWebSocket* webSocket){//删除套接字
    //此处要加锁
    m_mutex->lock();
    auto it=webSocketM.find(webSocket);
    webSocketM.erase(it);
    emit(this->sendC("山海鲸断开连接...",SendContent::DEL));//断开连接
    m_mutex->unlock();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_listenButton_clicked()
{
    if(ui->IPContent->displayText().isEmpty()||ui->WebSocketContent->displayText().isEmpty()||ui->TcpServerContent->displayText().isEmpty()){
        QMessageBox::warning(this,"连接失败","请输入全部内容");
        return;
    }
    QString IP=ui->IPContent->displayText();
    int webSocketServerPort=ui->WebSocketContent->displayText().toInt();
    int tcpServerPort=ui->TcpServerContent->displayText().toInt();
    //创建并运行websocket线程
    this->webSocketServer=new QWebSocketServer("server",QWebSocketServer::NonSecureMode,this);//等待山海鲸的连接
    connect( this->webSocketServer,&QWebSocketServer::newConnection,this,&MainWindow::newClient);
    if(!this->webSocketServer->listen(QHostAddress::Any,webSocketServerPort)){//监听本机的10000端口,用于和山海鲸通信
        perror("山海鲸监听出错:");
    }
    //运行tcpServer线程
    TcpServer* tcpServer=new TcpServer(IP,tcpServerPort);
    connect(tcpServer,&TcpServer::sendC,this,[=](QString s,SendContent t){//新连接TCp
        if(t==SendContent::CONNECT){
            ui->ConnectStu->append("<font color=green>"+s+"</font>");
        }else{
            ui->ConnectStu->append("<font color=red>"+s+"</font>");
        }
    });
    connect(tcpServer,&TcpServer::sendM,this,[=](QString s){//客户端TCP发送消息
        ui->AcceptContent->append("客户端: "+s);
    });
    connect(tcpServer,&TcpServer::sendWebSocket,this,[=](QString s){
        ui->SendContent->append("服务端: "+s);
    });
    ui->listenButton->setEnabled(false);
    ui->IPContent->setEnabled(false);
    ui->TcpServerContent->setEnabled(false);
    ui->WebSocketContent->setEnabled(false);
    tcpServer->start();

}

